from datetime import datetime
from django.core.mail import send_mail, send_mass_mail
from utils.response import *
from .sender import MailBot
from .serializers import AddresseeSerializer
from rest_framework.viewsets import ViewSet
from .models import Addressee
from utils.response import SuccessJsonResponse
from apps.user.models import SystemUser


def index(request):
	# message2 = ('Another Subject', 'Here is another message', 'zhanglei@jushangmei.com', ['190938605@qq.com','me_ericzhang@163.com'])
	# count = send_mass_mail((message2,), fail_silently=False)
	# print(f'count: {count}')

	subject = "HTML"
	message = '测试邮件'
	from_email = 'zhanglei@jushangmei.com'
	mails = ['190938605@qq.com', 'me_ericzhang@163.com']

	# MailBot().mass_test(subject, message, from_email, mails)
	MailBot().s_mail(subject, from_email, mails, text_content='test')
	return SuccessJsonResponse(msg='发送成功')


class AddresseeViewSet(ViewSet):
	serializer_class = AddresseeSerializer

	# 新建收件人信息
	def	create(self, request):
		"""
		:param str user_id: 用户id
		:param boole send_mail: 是否开启邮箱收件
		:parm boole send_msg: 是否开启短信息
		:return:
		"""
		user_id = request.data.get('user')
		send_mail = request.data.get('sendMail')
		send_msg = request.data.get('sendMsg')
		if user_id:
			user = SystemUser.objects.filter(id=user_id).first()
		else:
			return ErrorResponse(msg='参数错误: user是必传参数')

		if user and not Addressee.objects.filter(user=user).filter(deleted=False).exists():
			Addressee.objects.create(user=user, send_email=True if send_mail == '1' else False, send_message=True if send_msg == '1' else False)
		else:
			msg = '用户不存在或已添加'
			return ErrorResponse(msg=msg)
		return SuccessResponse()

	def	delete(self, request):
		addressee_id = request.data.get('id')
		if addressee_id:
			addressee = Addressee.objects.filter(id=addressee_id)
		else:
			return ErrorResponse(msg='参数错误: id是必传参数')
		if addressee:
			addressee.update(deleted=True, update_time=datetime.now())
		else:
			msg = '用户不存在'
			return ErrorResponse(msg=msg)
		return SuccessResponse()

	def update(self, request):
		addressee_id = request.data.get('id')
		if not addressee_id :
			return ErrorResponse(msg='参数错误: [id]是必传参数')
		send_mail = request.data.get('sendMail')
		send_msg = request.data.get('sendMsg')

		addressee = Addressee.objects.filter(id=addressee_id, deleted=False)
		if addressee:
			addressee.update(send_email=True if send_mail == '1' else False, send_message=True if send_msg == '1' else False, update_time=datetime.now())
			return SuccessResponse(msg='更新成功')
		else:
			return ErrorResponse(msg='修改项不存在')

	def list(self, request):
		addressees = Addressee.objects.filter(deleted=False)
		data = []
		for addressee in addressees:
			data.append({
				'id': addressee.id,
				'username': addressee.user.username,
				'mobile': addressee.user.mobile,
				'email': addressee.user.email,
				'send_mail': addressee.send_email,
				'send_message': addressee.send_message,
			})

		return SuccessResponse(msg='调用成功', data={'list': data})

	def get_by_id(self, request):
		id = request.query_params.get('id')
		if id:
			addressee = Addressee.objects.filter(id=id, deleted=False).first()

			if addressee:
				data = [{
					'id': addressee.id,
					'username': addressee.user.username,
					'mobile': addressee.user.mobile,
					'email': addressee.user.email,
					'send_mail': addressee.send_email,
					'send_message': addressee.send_message,
				}]
				return SuccessResponse(msg='调用成功', data={'list': data})
			else:
				return SuccessResponse(data=[])
		else:
			return SuccessResponse(data=[])
